import Toast from 'tdesign-miniprogram/toast/index';

Page({
  data: {
    confirmBtn: { content: '确定', variant: 'base' },
    dialogKey: '',
    showConfirm: false,
    showPopup: false,
    visible: false,
    activeValues: [0],
    list: [],
    current: {},
  },

  goodListPagination: {
    index: 0,
    num: 20,
  },

  privateData: {
    tabIndex: 0,
  },

  onLoad() {
    const personId = wx.getStorageSync('personId');

    wx.request({
      url: `http://8.130.99.108:8090/api/getAppointmentInfo`, //仅为示例，并非真实的接口地址
      data: {
        doctorId: personId + '',
        pageSize: 100,
        pageNum: 1,
      },
      method: 'GET',
      success: (res) => {
        this.setData({ list: res.data.data.list });
      },
      fail: (error) => {
        console.log(error);
      },
    });
  },

  onShow() {
    this.getTabBar().init();
  },

  onClose({ detail }) {
    console.log(detail.trigger);
  },
  showDialog(e) {
    const { key, item } = e.currentTarget.dataset;
    console.log(item);
    this.setData({ [key]: true, dialogKey: key });
  },
  closeDialog() {
    const { dialogKey } = this.data;
    this.setData({ [dialogKey]: false });
  },
  confirmPre() {
    const { dialogKey } = this.data;
    this.setData({ [dialogKey]: false });
    // wx.request({
    //   url: `http://8.130.99.108:8090/api/getBaseInfo`, //仅为示例，并非真实的接口地址
    //   data: {
    //     doctorId: personId + '',
    //     pageSize: 100,
    //     pageNum: 1,
    //   },
    //   method: 'GET',
    //   success: (res) => {
    //     this.setData({ list: res.data.data.list });
    //   },
    //   fail: (error) => {
    //     console.log(error);
    //   },
    // });
    Toast({
      context: this,
      selector: '#t-toast',
      message: '取消预约成功',
    });
  },
  onVisibleChange(e) {
    this.setData({
      visible: e.detail.visible,
    });
  },

  handlePopup(e) {
    const current = e.currentTarget?.dataset?.item;

    const header = `${current.personName}-${current.department}-${current.appointmentTime}`;

    let newArr = {
      ...current,
      visitedHistoryList: current.visitedHistoryList.map((item) => ({
        ...item,
        header,
      })),
    };

    console.log(newArr);

    this.setData({
      visible: true,
      current: newArr,
    });
  },

  handleChange(e) {
    this.setData({
      activeValues: e.detail.value,
    });
  },
});
